<?php
require(dirname(__FILE__)."/config.php");
require_once RSMSINC.'/misc.func.php';
require_once(PHPEXCELINC.'/phpexcel.class.php');
include PHPEXCELINC.'/Writer/Excel5.php';
/** PHPExcel_RichText */
require_once PHPEXCELINC.'/RichText.php';
set_time_limit(0);
$aid_array = array ();
$curtype = "";
if(isset($_GET['aids']))
{ 
	$aids=$_GET['aids'];
	$aid_array = explode(',', $aids);
	$curtype = "sel";
} else {
	$curtype = $type;
}
$objPHPExcel = new PHPExcel();
$objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->duplicateStyleArray(
		array(
			'font'    => array(
				'bold'      => true,
				'size'		=> 12
			),
			'alignment' => array(
				'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
			),
			'borders' => array(
				'top'     => array(
 					'style' => PHPExcel_Style_Border::BORDER_THIN
 				)
			),
			'fill' => array(
	 			'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
	  			'rotation'   => 90,
	 			'startcolor' => array(
	 				'argb' => 'FFA0A0A0'
	 			),
	 			'endcolor'   => array(
	 				'argb' => 'FFFFFFFF'
	 			)
	 		)
		),
		'A3:G3'
);
for ($i = ord('A'); $i <= ord('Z'); $i++) {
	$objActSheet->getColumnDimension(chr($i))->setWidth(20);
}
if ($curtype == "period")
	$objActSheet->getColumnDimension(chr(ord('F')))->setWidth(40);
$objActSheet->setCellValue('A3', mb_convert_encoding('用户名', "GB2312", "UTF-8"));
$objActSheet->setCellValue('B3', mb_convert_encoding('用户所在公司', "GB2312", "UTF-8"));
if ($curtype == "sel" || $curtype == "period")
	$objActSheet->setCellValue('C3', mb_convert_encoding('登录时间', "GB2312", "UTF-8"));
else
	$objActSheet->setCellValue('C3', mb_convert_encoding('最后登录时间', "GB2312", "UTF-8"));
$objActSheet->setCellValue('D3', mb_convert_encoding('登录IP', "GB2312", "UTF-8"));
$objActSheet->setCellValue('E3', mb_convert_encoding('IP所在位置', "GB2312", "UTF-8"));
if ($curtype == "period")
{
	$objActSheet->setCellValue('F3', mb_convert_encoding("从".$starttime."到".$endtime."总登录次数", "GB2312", "UTF-8"));
} else if ($curtype == "total") {
	$objActSheet->setCellValue('F3', mb_convert_encoding('本月登录次数', "GB2312", "UTF-8"));
	$objActSheet->setCellValue('G3', mb_convert_encoding('总登录次数', "GB2312", "UTF-8"));
}
$cell_row = 4;
$cell_row_s;
if ($curtype == "sel") {
	foreach ($aid_array as $aid)
	{
		$sql = "Select l.username,l.logintime,l.loginip,m.company From `#@__loginlog` l left join `#@__member` m on m.userid=l.username where id=$aid order by id desc";
		$dsql->ExecuteNoneQuery ($sql);
		$dsql->Execute();
		while($row = $dsql->GetArray())
		{
			$cell_col = ord('A');
			$cell_row_s = sprintf ("%d", $cell_row);
			
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['username'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['company'], "GB2312", "UTF-8"));
							
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(GetDateTimeMk($row['logintime']), "GB2312", "UTF-8"));
	
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['loginip'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(ip2location($row['loginip']), "GB2312", "UTF-8"));
				
			$cell_row++;
		}
	}
} else if ($curtype == "total") {
	$lsql = new RsSql(false);
	$firstdate = GetDateMk(time());
	$firstdate = substr_replace ($firstdate, "01", strrpos($firstdate, "-")+1, 2);
	$firstdatemk = GetMkTime($firstdate);
	$sql = "Select l.username,l.logintime, max(l.logintime) as mlogintime,l.loginip,m.company, count(*) as times From `#@__loginlog` l left join `#@__member` m on m.userid=l.username group by username";
		$dsql->ExecuteNoneQuery ($sql);
		$dsql->Execute();
		while($row = $dsql->GetArray())
		{
			$cell_col = ord('A');
			$cell_row_s = sprintf ("%d", $cell_row);
			
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['username'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['company'], "GB2312", "UTF-8"));
							
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(GetDateTimeMk($row['mlogintime']), "GB2312", "UTF-8"));
	
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['loginip'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(ip2location($row['loginip']), "GB2312", "UTF-8"));
				
			//本月登录次数
			$lrow = $lsql->GetOne ("Select count(*) as monthtimes From `#@__loginlog` where username='$row[username]' And 
				logintime > $firstdatemk");
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
			$cell_idx, mb_convert_encoding($lrow['monthtimes'], "GB2312", "UTF-8"));
				
			//总登录次数
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['times'], "GB2312", "UTF-8"));
				
			$cell_row++;
		}
	$lsql->Close();
} else if ($curtype == "period") {
	$begindate = GetMkTime($starttime);
	$enddate = GetMkTime($endtime);
	
	$sql = "Select l.username,l.logintime, max(l.logintime) as mlogintime,l.loginip,m.company, count(*) as times From `#@__loginlog`  l left join `#@__member` m on m.userid=l.username where l.logintime > $begindate and l.logintime < $enddate group by username";
	
	$dsql->ExecuteNoneQuery ($sql);
		$dsql->Execute();
		while($row = $dsql->GetArray())
		{
			$cell_col = ord('A');
			$cell_row_s = sprintf ("%d", $cell_row);
			
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['username'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['company'], "GB2312", "UTF-8"));
							
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(GetDateTimeMk($row['mlogintime']), "GB2312", "UTF-8"));
	
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['loginip'], "GB2312", "UTF-8"));
				
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding(ip2location($row['loginip']), "GB2312", "UTF-8"));
				
				
			//总登录次数
			$cell_col++;
			$cell_idx = chr($cell_col).$cell_row_s;
			$objActSheet->setCellValue(
				$cell_idx, mb_convert_encoding($row['times'], "GB2312", "UTF-8"));
				
			$cell_row++;
		}
}
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
header('Content-type: text/csv"');
header('Content-Disposition: attachment;filename="userlogin_info.xls"');
//header('Cache-Control: max-age=0');
header('Cache-Control: must-revalidate, post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save('userlogin_info.xls');
$objWriter->save('php://output');
ShowMsg("成功生成EXCEL文档！","userlogin_info.php");
?>